GoUni.controller('SearchCtrl', function ($scope, DataService) {
	
	$scope.translations = {
		search: 'Search',
		all: 'All',
		location: 'Location',
		rank: 'Rank',
		ielts:'Minimum IELTS',
		tuition:'Tuition'
	};
	
	$scope.filters = {
		q:'',
		loc:'',
		tuition:0,
		rank:0,
		ielts:0,
		page:1,
	};
	
	
	$scope.total_items = -1;
	$scope.items_per_page = 10;

  	$scope.init = function() {
		var locations_url = '/api/locations/';
		DataService.get(locations_url).then(function (data) {
			$scope.locations = data;
		}, function(status){
			console.log(status);
		});
			
	};
	

	$scope.search = function() {
		
		var none_empty_filters = _.object(
			_.filter(
				_.pairs($scope.filters), 
				function(filter){
					return (filter[1]!='') && (filter[1]!=0);
				}
			)
		);
		
		var search_url = '/api/search/?'+ encode_query_data(none_empty_filters) ;
		
		DataService.get(search_url).then(function (data) {
			$scope.universities = data.universities;
			$scope.total_items = data.total_items;
			$scope.items_per_page = data.items_per_page;
		}, function(status){
			console.log(status);
		});
			
	};
	
	$scope.page_changed = function() {
		$scope.search();
	};
	
	$scope.display_value = function(value) {
		return value == 0 ? 'All' : value;
	};
	
});


